<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/22
 * Time: 9:55
 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */

namespace app\admin\controller;

use app\home\model\HomeModule;

use app\home\model\HomeTemplate;
use app\Request;


class Module
{


    public function moduleList(Request $request)
    {
        $param = $request->param();
        $moduleModel = new HomeModule();
        $limit  =  10;
        $start = 0;
        if (isset($param['limit']) &&  !empty($param['limit'])){
            $limit = $param['limit']  ;
        }
        if (isset($param['page']) &&  !empty($param['page'])){
            $start = ($param['page'] -1)  *  $limit;
        }
        $where['type'] = 1;
        if (isset( $param['type']) && !empty($param['type']) ){
            $where['type'] = $param['type'];
        }
        if (isset( $param['template_id']) && !empty($param['template_id']) ){
            $where['template_id'] = $param['template_id'];
        }else{
            if ( $where['type'] != 3){
                $homeTemplateModel = new HomeTemplate();
                $template = $homeTemplateModel->where(['status' => 1])->find();
                $where['template_id'] = $template['id'];
            }

        }
        //$where['template_id'] = $param['template_id'];

        $data = $moduleModel->order('sort', 'asc')->limit($start,$limit)->where($where)->select()->toArray();
        $count = $moduleModel->order('sort', 'asc')->where($where)->count();
        return adminSuccess('模块', ['list' => $data,'count' => $count]);
    }

    public function upModuleStatus(Request $request)
    {
        $param = $request->param();
        $homeTemplateModel = new HomeModule();
        $homeTemplateModel->where(['id' => $param['id']])->update(['status' => $param['status']]);
        return adminSuccess('修改成功');
    }

    public function editModule(Request $request)
    {
        $param = $request->param();
        $homeTemplateModel = new HomeModule();
        $data =
            [
                'name' => $param['name'],
                'icon' => $param['icon'],
                'link' => $param['link'],
                'sort' => $param['sort'],
                'desc' => $param['desc'],
                'update_time' => time()
            ];
        $homeTemplateModel->where(['id' => $param['id']])->update($data);
        return adminSuccess('修改成功');
    }
}